using System;
using System.Collections.Generic;
using System.Linq;
namespace WebClient.Controllers
{
	#region using
	using System.Web.Mvc;
	using WebClient.Models.ViewModels;
	using WebClient.Models;
	using WebClient.Models.Repositories;
	using WebClient.Helpers;
	using System.Web.Security;
    using WebClient.CF_DAL_WS;
	#endregion
	public partial class AdminController: BaseController
	{
        public ActionResult AdminSelectOrders()
		{
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS
			};
            return View("AdminSelectOrders", CreateViewModel(data));
		}

        public ActionResult AdminSelectOrdersPaging(int page = 1)
        {
            var data = new DataTransferViewModel
            {
                EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
                CurrentPage = page
            };
            return PartialView("Templates/TH_ListOrdersForAdmin", CreateViewModel(data));
        }

        public ActionResult SelectOrdersByIDRestaurant(int idrestaurant)
        {
            var data = new DataTransferViewModel
            {
                IDRestaurant = idrestaurant,
                EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
            };
            return View("SelectOrders", CreateViewModel(data));
        }

		public ActionResult SelectOrdersByIDUsers(Guid idUsers)
		{
			var data = new DataTransferViewModel
			{
				IDUsers = idUsers,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
			};
			return View("SelectOrders", CreateViewModel(data));
		}

		public ActionResult SelectOrdersByIDUsersPaging(Guid idUsers)
		{
			var data = new DataTransferViewModel
			{
				IDUsers = idUsers,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
			};
			return PartialView("Templates/TH_ListOrders", CreateViewModel(data));
		}


		public ActionResult DeleteOrders(int id, int idrestaurant, int page = 1)
		{
			var result = _repOrders.Delete(id);
			var data = new DataTransferViewModel
			{
                IDRestaurant = idrestaurant,
				InfoText = result ? "Item has been deleted" : "Cannot delete item!",
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
				CurrentPage = page
			};
			return View("SelectOrders", CreateViewModel(data));
		}

		public ActionResult EditOrders(int id, int idrestaurant, int page)
		{
			var data = new DataTransferViewModel
			{
                IDRestaurant = idrestaurant,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
				CurrentPage = page,
				IDOrder = id,
                IntID = id
			};
            return View("EditOrders", CreateViewModel(data));
		}

        public ActionResult CancelEditingOrders(int idrestaurant, int page)
		{
			var data = new DataTransferViewModel
			{
                IDRestaurant = idrestaurant,
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
				CurrentPage = page
			};
            return View("EditOrders", CreateViewModel(data));
		}

		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult UpdateOrders(FormCollection forms)
		{
			var orders = _repOrders.SelectByID(int.Parse(forms.GetValues("Orders_ID")[0]));
			orders.ShipDate = forms.GetValues("Orders_ShipDate")[0];
            orders.IsVerified = bool.Parse(forms.GetValues("Orders_IsVerified")[0]);
            orders.IsShipped = bool.Parse(forms.GetValues("Orders_IsShipped")[0]);
			var result = _repOrders.Update(orders);
			var data = new DataTransferViewModel
			{
                IDRestaurant = int.Parse(forms.GetValues("Orders_IDRestaurant")[0]),
                CurrentPage = int.Parse(forms.GetValues("Orders_CurrentPage")[0]),
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
			};
			return View("SelectOrders", CreateViewModel(data));
		}


		[ValidateInput(false)]
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult InsertOrders(FormCollection forms)
		{
			string idUsers = forms.GetValues("Orders_IDUsers")[0];
			string orderdate = forms.GetValues("Orders_OrderDate")[0];
			var newItem = new OrdersDTO
			{
                IDUsers = idUsers,
				OrderDate = orderdate
			};
			var result = _repOrders.Insert(newItem);
			var data = new DataTransferViewModel
			{
				EnumViewModelType = EnumViewModel.ADMIN_ORDERS,
				InfoText = result != -1 ? "New item has been added" : "Cannot insert new item"
			};
			return View("SelectOrders", CreateViewModel(data));
		}

        public ActionResult Revenue(int idrestaurant)
        {
            var data = new DataTransferViewModel
            {
                IDRestaurant = idrestaurant,
                EnumViewModelType = EnumViewModel.ADMIN_ORDERS_REVENUE,
            };
            return View("Revenue", CreateViewModel(data));
        }

        [ValidateInput(false)]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Revenue(FormCollection forms)
        {
            var idrestaurant = forms.GetValues("Revenue_Restaurant")[0];
            var from = forms.GetValues("Revenue_From")[0];
            var to = forms.GetValues("Revenue_To")[0];
            var data = new DataTransferViewModel
            {
                From = DateTime.Parse(from),
                To = DateTime.Parse(to),
                IDRestaurant = int.Parse(idrestaurant),
                EnumViewModelType = EnumViewModel.ADMIN_ORDERS_REVENUE_EXECUTE,
            };
            return View("Revenue", CreateViewModel(data));
        }
	}
}